﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Diagnostics;
using System.ComponentModel;
using WPFExcelReport.Helpers;
using System.Windows.Input;
using System.Collections.Generic;
namespace WPFExcelReport
{
    public partial class TWSCO
    {
        #region ==VAAUF===========
        public static readonly DependencyProperty VAAUFProperty = DependencyProperty.Register("VAAUF",
            typeof(string), typeof(TWSCO),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVAAUFChanged));
        public string VAAUF
        {
            get
            {
                return (string)GetValue(VAAUFProperty);
            }
            set
            {
                SetValue(VAAUFProperty, value);
                OnVAAUFChg();
            }
        }
        private static void OnVAAUFChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWSCO;
            if (src != null)
            {
                src.VAAUF = (string)e.NewValue;
            }
        }
        public virtual void OnVAAUFChg()
        { }
        #endregion
        #region ==SAAUF===========
        public static readonly DependencyProperty SAAUFProperty = DependencyProperty.Register("SAAUF",
            typeof(string), typeof(TWSCO),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSAAUFChanged));
        public string SAAUF
        {
            get
            {
                return (string)GetValue(SAAUFProperty);
            }
            set
            {
                SetValue(SAAUFProperty, value);
                OnSAAUFChg();
            }
        }
        private static void OnSAAUFChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWSCO;
            if (src != null)
            {
                src.SAAUF = (string)e.NewValue;
            }
        }
        public virtual void OnSAAUFChg()
        { }
        #endregion
        #region ==WAAUF===========
        public static readonly DependencyProperty WAAUFProperty = DependencyProperty.Register("WAAUF",
            typeof(string), typeof(TWSCO),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWAAUFChanged));
        public string WAAUF
        {
            get
            {
                return (string)GetValue(WAAUFProperty);
            }
            set
            {
                SetValue(WAAUFProperty, value);
                OnWAAUFChg();
            }
        }
        private static void OnWAAUFChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWSCO;
            if (src != null)
            {
                src.WAAUF = (string)e.NewValue;
            }
        }
        public virtual void OnWAAUFChg()
        { }
        #endregion 
        #region ==KAAUF===========
        public static readonly DependencyProperty KAAUFProperty = DependencyProperty.Register("KAAUF",
            typeof(string), typeof(TWSCO),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnKAAUFChanged));
        public string KAAUF
        {
            get
            {
                return (string)GetValue(KAAUFProperty);
            }
            set
            {
                SetValue(KAAUFProperty, value);
                OnKAAUFChg();
            }
        }
        private static void OnKAAUFChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWSCO;
            if (src != null)
            {
                src.KAAUF = (string)e.NewValue;
            }
        }
        public virtual void OnKAAUFChg()
        { }
        #endregion 
        public TWSCO()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWASTAMM();
            this.DataContext = MV;
            InitializeComponent();
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
        }


        public override void LOADBYAUF()
        {
            base.LOADBYAUF();

        }
        public override void OnEXAUFChg(string vlaue)
        {
            VAAUF = "";
            SAAUF = "";
            WAAUF = "";
            KAAUF = "";
            if (vlaue.Length == 0)
                return;
            TMV.MCO.dwData = TMV.MCO.TBMain.NewRow();
            TMV.MCO.dwData["MANDANT"] = TMV.MCO.app.Mandant;
            TMV.MCO.dwData["KRS"] = "KA";
            TMV.MCO.dwData["ART"] = vlaue.Substring(0, 20);
            TMV.MCO.dwData["VAR"] = "   ";
            TMV.MCO.dwData["FARBE"] = "1".PadRight(10);
            TMV.MCO.SResult = TMV.MCO.QMCO04(TMV.MCO.dwData);
            foreach (DataRow row in TMV.MCO.SResult.Tables[0].Rows)
            {
                string krs = row["KRS"].ToString();
                string lfd = row["LFD"].ToString();
                string pos = row["POS"].ToString();
                Dictionary<string, string> list = (Dictionary<string, string>)TMV.TAB.GETON(krs, lfd, pos);
                string key = krs + "_" + lfd + "_" + pos;
                list.Add(key, "YES");
                foreach (KeyValuePair<string, string> kp in list)
                {
                    key = kp.Key;
                    krs = key.Substring(0, 2);
                    lfd = key.Substring(3, 6);
                    pos = key.Substring(10, 3); 
                    switch (krs)
                    {
                        case "KA":
                            KAAUF = "V" + krs + lfd + pos;
                            break;
                        case "WA":
                            WAAUF = "V" + krs + lfd + pos;
                            break;
                        case "SA":
                            SAAUF = "V" + krs + lfd + pos;
                            break;
                        case "VA":
                            VAAUF = "K" + krs + lfd + row["POS"].ToString();
                            break;
                    }
                }
                break;
            }
        }
    }
}
